package org.abacus.platform.util;

import java.io.UnsupportedEncodingException;

public enum Coder {
	UFT8("UTF-8"), GBK("GBK"), ISO88591("ISO-8859-1");

	private String charsetName;

	private Coder(String charsetName) {
		this.charsetName = charsetName;
	}

	public String charsetName() {
		return charsetName;
	}

	public byte[] encode(String string) {
		try {
			return string.getBytes(charsetName());
		} catch (UnsupportedEncodingException e) {
			throw new Error(charsetName() + " should be available on all JVMs", e);
		}
	}

	public String decode(byte[] bytes) {
		try {
			return new String(bytes, charsetName());
		} catch (UnsupportedEncodingException e) {
			throw new Error(charsetName() + " should be available on all JVMs", e);
		}
	}
}
